%% Producing Figure 7: bank run dynamics

% To reproduce bank capital and leverage in Figure 7,
% please download the data first following the instruction given in "README.txt".

close all

load('no_policy')

%% Quarterly bank loan data.

% Data is column B.
% 2008Q4 is Column 259. 247 to 279 is 2005Q4 to 2013Q4.
% Take a log-linear trend in 2005Q4-2008Q3, and compute the gap from this log-linear trend.
data_loan = readmatrix('bank_loan_q.xlsx','Sheet','FRED Graph','Range','b247:b279');
log_data_loan = log(data_loan);
% Take a log-linear trend of 3 years before 2008Q4.
[log_trend,cyc] = hpfilter(log_data_loan(1:12),Smoothing=1000000000);
% Extraporate the log-linear trend until 2013Q4.
gap = log_trend(2) - log_trend(1);
log_trend = vertcat(log_trend,zeros(size(data_loan,1)-12,1));
for t=1:size(data_loan,1)-12
    log_trend(12+t) = log_trend(11+t) + gap;
end
% Compute deviations from the log-linear trend
log_gap = log_data_loan - log_trend;

%% Take trend since 2000Q4

% 2008Q4 is Column 259. 227 to 279 is 2000Q4 to 2013Q4.
% Take a log-linear trend in 2000Q4-2008Q3, and compute the gap from this log-linear trend.
data_loan = readmatrix('bank_loan_q.xlsx','Sheet','FRED Graph','Range','b227:b279');
log_data_loan = log(data_loan);
% Take a log-linear trend of 8 years before 2008Q4.
[log_trend,cyc] = hpfilter(log_data_loan(1:32),Smoothing=1000000000);
% Extraporate the log-linear trend until 2013Q4.
gap = log_trend(2) - log_trend(1);
log_trend = vertcat(log_trend,zeros(size(data_loan,1)-32,1));
for t=1:size(data_loan,1)-32
    log_trend(32+t) = log_trend(31+t) + gap;
end
% Compute deviations from the log-linear trend
log_gap = log_data_loan - log_trend;

% Match with the model dynamics of i
figure('Units','Inches','OuterPosition',[7 0 8 8],'Name','Bank lending')
plot(-run_before:run_after,run2_avg_i./avg_i_nopol*100-100,'Linewidth',4)
hold on
plot(-run_before:run_after,i_b./avg_i_nopol*100-100,'b--','Linewidth',3)
hold on
plot(-run_before:run_after,i_a./avg_i_nopol*100-100,'b--','Linewidth',3)
hold on
plot(-run_before:run_after,log_gap(33-run_before:33+run_after)*100,'r-.','Linewidth',3);
hold on
plot(-run_before:run_after,zeros(1,1+run_before+run_after),'k:','Linewidth',3);
hold on
plot([0 0],[-30 30],'k-.','Linewidth',3);
set(gca,'FontSize',40);
xlim([-run_before run_after])
xticks(-run_before:4:run_after)
ylim([-30 30]);
yticks(-30:10:30)
grid on
ytickformat('percentage')
xtickangle(0)

%% Take trend since 1990Q3

% 2008Q4 is Column 259. 227 to 279 is 2000Q4 to 2013Q4.
% Take a log-linear trend in 2000Q4-2008Q3, and compute the gap from this log-linear trend.
data_loan = readmatrix('bank_loan_q.xlsx','Sheet','FRED Graph','Range','b187:b279');
log_data_loan = log(data_loan);
% Take a log-linear trend of 18 years before 2008Q4.
[log_trend,cyc] = hpfilter(log_data_loan(1:72),Smoothing=1000000000);
% Extraporate the log-linear trend until 2013Q4.
gap = log_trend(2) - log_trend(1);
log_trend = vertcat(log_trend,zeros(size(data_loan,1)-72,1));
for t=1:size(data_loan,1)-72
    log_trend(72+t) = log_trend(71+t) + gap;
end
% Compute deviations from the log-linear trend
log_gap = log_data_loan - log_trend;

%% Quaterly bank leverage: bank asset divided by bank capital.

% Data is column B.
% 2008Q4 is Column 112 for both asset and equity.
data_asset   = readmatrix('bank_asset.xlsx',  'Sheet','FRED Graph','Range','b100:b132');
data_capital = readmatrix('bank_capital.xlsx','Sheet','FRED Graph','Range','b100:b132');

data_lev = data_asset ./ data_capital;

%% Spread

% b12:b93 is 2003Q3-2023Q4.
% 2006Q3 is b24 (13).
data_baa    = readmatrix('BAA10Y.xlsx','Range','b12:b93');
avg_baa     = mean(data_baa);
plot_spread = data_baa-avg_baa;
start_t = 14;
end_t   = 14+run_before+run_after;

figure('Units','Inches','OuterPosition',[7 0 8 8],'Name','Spread')
plot(-run_before:run_after,10000*(run2_avg_spread-avg_spread_nopol),'Linewidth',4)
hold on
plot(-run_before:run_after,10000*(spread_b-avg_spread_nopol),'b:','Linewidth',3)
hold on
plot(-run_before:run_after,10000*(spread_a-avg_spread_nopol),'b:','Linewidth',3)
hold on
plot(-run_before:run_after,plot_spread(start_t:end_t)*100,'r-.','Linewidth',4)
hold on
plot(-run_before:run_after,zeros(1,1+run_before+run_after),'k:','Linewidth',2);
hold on
plot([0 0],[-200 400],'k:','Linewidth',2);
set(gca,'FontSize',40);
xlim([-run_before run_after])
xticks(-run_before:4:run_after)
ylim([-200 400]);
yticks(-200:100:400)
grid on
xtickangle(0)

%% Run Probability

figure('Units','Inches','OuterPosition',[7 0 8 8],'Name','Run probability')
plot(-run_before:run_after,run2_avg_pr*100,'Linewidth',4)
hold on
plot(-run_before:run_after,pr_b*100,'b--','Linewidth',3)
hold on
plot(-run_before:run_after,pr_a*100,'b--','Linewidth',3)
hold on
%plot(-run_before:run_after,ones(1,1+run_before+run_after)*avg_pr_nopol*100,'k:','Linewidth',2);
%hold on
plot(-run_before:run_after,ones(1,1+run_before+run_after)*stss_pr*100,'k:.','Linewidth',3);
hold on
plot([0 0],[0 40],'k-.','Linewidth',3);
set(gca,'FontSize',40);
xlim([-run_before run_after])
xticks(-run_before:4:run_after)
ylim([0 40]);
yticks(0:10:40)
grid on
ytickformat('percentage')
xtickangle(0)

return
% Code below is to reproduce Panel (d) leverage and (b) bank capital in Figure 7.
% We used the XLF index downloaded from the Bloomberg terminal.
% Please see "README.txt" for the data source.

%% Quaterly bank leverage: bank asset divided by XLF-adjusted bank capital data.

% Data is column B.
% 2008Q4 is row 112 for both asset and equity.
% Use 2005Q4 XLF index for normalization.
data_asset   = readmatrix('bank_asset.xlsx',  'Sheet','FRED Graph','Range','b100:b132');
data_capital = readmatrix('bank_capital.xlsx','Sheet','FRED Graph','Range','b100:b132');
data_xlf     = readmatrix('xlf.xlsx',         'Sheet','quarterly', 'Range','b20:b52');

cap_xlf_ratio = data_capital./data_xlf(1);
cap_xlf       = cap_xlf_ratio.*data_xlf;
xlf_lev       = data_asset ./ cap_xlf;

figure('Units','Inches','OuterPosition',[7 0 8 8],'Name','Leverage')
plot(-run_before:run_after,run2_avg_l,'Linewidth',4)
hold on
plot(-run_before:run_after,l_b,'b--','Linewidth',3)
hold on
plot(-run_before:run_after,l_a,'b--','Linewidth',3)
hold on
plot(-run_before:run_after,xlf_lev(13-run_before:13+run_after),'r-.','Linewidth',3)
hold on

plot(-run_before:run_after,ones(1,1+run_before+run_after)*stss_l,'k:','Linewidth',2);
hold on
plot([0 0],[-100 100],'k:','Linewidth',2);
xlim([-run_before run_after])
xticks(-run_before:4:run_after)
ylim([6 40]);
grid on
set(gca,'FontSize',40);
%title('Bank asset/Bank equity (XLF)')
xtickangle(0)

%% Quaterly bank leverage: 2005Q4 capital and % changes only by XLF

% Data is column B.
% 2008Q4 is Column 112 for both asset and equity.
% Use 2005Q4 XLF index for normalization.
data_asset   = readmatrix('bank_asset.xlsx',  'Sheet','FRED Graph','Range','b100:b132');
data_capital = readmatrix('bank_capital.xlsx','Sheet','FRED Graph','Range','b100:b132');
data_xlf     = readmatrix('xlf.xlsx',         'Sheet','quarterly', 'Range','b20:b52');

cap_xlf_ratio = data_capital(1)/data_xlf(1);
cap_xlf       = cap_xlf_ratio*data_xlf;
xlf_lev       = data_asset ./ cap_xlf;

%% Bank capital dynamics using XLF

% Data is column B.
% 2002/12/31 is the initial period, which is row 7.
% 2008Q4 is row 32, which is 26 in the imported data_xlf.
% 2024/ 9/30 is the last period, which is row 95.
data_xlf = readmatrix('xlf.xlsx',  'Sheet','quarterly','Range','b7:b95');

% Bank capital
% 77 is 2000Q1. 172 is 2023Q4.
% 2008Q4 is 112.
data_cap = readmatrix('bank_capital.xlsx','Sheet','FRED Graph','Range','b77:b172');
log_data_cap = log(data_cap);
[cap_linear_trend,cap_cyc] = hpfilter(log_data_cap,Smoothing=1600);

% XLF % deviations from 2005Q4, which is row 14.
figure('Units','Inches','OuterPosition',[7 0 8 8],'Name','Bank capital')
plot(-run_before:run_after,run2_avg_n./avg_n_nopol*100-100,'Linewidth',4)
hold on
plot(-run_before:run_after,n_b./avg_n_nopol*100-100,'b--','Linewidth',3)
hold on
plot(-run_before:run_after,n_a./avg_n_nopol*100-100,'b--','Linewidth',3)
hold on
plot(-run_before:run_after,(data_xlf(26-run_before:26+run_after)./data_xlf(14)-1)*100,'r-.','Linewidth',3);
hold on

plot(-run_before:run_after,zeros(1,1+run_before+run_after),'k:','Linewidth',3);
hold on
plot([0 0],[-100 100],'k:','Linewidth',2);
set(gca,'FontSize',40);
xlim([-run_before run_after])
xticks(-run_before:4:run_after)
ylim([-80 60]);
yticks(-80:20:60)
grid on
ytickformat('percentage')
xtickangle(0)
